/*
  @author NormanMises
  @date 2023/6/29 - 14:59
*/

package com.example.demo.controller;

import java.util.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
@RequestMapping("/download2")
public class DownloadController2 {
    @PostMapping("/")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                String fileName = file.getOriginalFilename();
                File newFile = new File("E:\\answer\\daan\\" + fileName);
                FileOutputStream outputStream = new FileOutputStream(newFile);
                outputStream.write(bytes);
                outputStream.close();
                System.out.println("文件上传成功：" + fileName);
                return "文件上传成功";
            } catch (IOException e) {
                e.printStackTrace();
                return "文件上传失败：" + e.getMessage();
            }
        }else {
            return "文件为空";
        }
    }
}
